package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebLinkEntity;
import io.renren.modules.generator.service.WebLinkService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 友情链接表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-24 12:56:48
 */
@RestController
@RequestMapping("generator/weblink")
@Api(value = "友情链接",tags = {"友情链接"})
public class WebLinkController {
    @Autowired
    private WebLinkService webLinkService;


    @Autowired
    private SysUserService sysUserService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:weblink:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webLinkService.queryPage(params);
        List<WebLinkEntity> list = (List<WebLinkEntity>) page.getList();
        for (WebLinkEntity webLinkEntity : list) {
            Integer createUser = webLinkEntity.getCreateUser();
            SysUserEntity sysUserServiceById = sysUserService.getById(createUser);
            webLinkEntity.setUserName(sysUserServiceById.getUsername());
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{linkId}")
    @RequiresPermissions("generator:weblink:info")
    public R info(@PathVariable("linkId") Integer linkId){
		WebLinkEntity webLink = webLinkService.getById(linkId);

        return R.ok().put("webLink", webLink);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:weblink:save")
    public R save(@RequestBody WebLinkEntity webLink){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
        webLink.setCreateUser(Math.toIntExact(userId));
        webLink.setCreateTime(new Date());
        webLink.setUpdateTime(new Date());
//        webLink.setLinkStatus(0);
//        webLink.setDelFlag(0);
		webLinkService.save(webLink);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:weblink:update")
    public R update(@RequestBody WebLinkEntity webLink){
        webLink.setUpdateTime(new Date());
		webLinkService.updateById(webLink);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:weblink:delete")
    public R delete(@RequestBody Integer[] linkIds){
		webLinkService.removeByIds(Arrays.asList(linkIds));

        return R.ok();
    }

}
